<?php
/**
 * Created by dutuwang.net
 * User: adophper <hello@adophper.com>
 * Date: 2018/4/8
 * Time: 18:26
 */
use yii\helpers\Html;
use yii\bootstrap\ActiveForm;

$this->title = Yii::t('frontend', 'my_info');
$tmp_name = $province_id = $city_id = '';
$tmp = \common\helpers\TStringHelper::getUserProvince($model->city, $province_id, $city_id, $tmp_name);
?>
<?=Html::cssFile('/static/frontend/css/center.css')?>
<div class="contBox">
    <div class="center_box row">
        <?=$this->render('left')?>
        <div class="col-md-10 right_min">
            <div class="right_box">
                <div class="right_title flex f_between">
                    <span class="right_t_label t_red">个人资料</span>
                    <span class="right_btn" id="edit_user_btn"><i class="glyphicon glyphicon-pencil"></i><em>修改个人资料</em></span>
                </div>
                <div class="right_item" id="original_user">
                    <div class="row">
                        <div class="col-md-2">
                            <span class="right_icon"><img src="<?=$model->avatar?>" onerror="this.src='/static/frontend/image/noavatar.png';"/></span>
                        </div>
                        <div class="col-md-10">
                            <ul class="right_item_list">
                                <li>用户名：<?=$model->username?></li>
                                <li>昵称：<?=$model->nickname?></li>
                                <li>个性签名：<?=$model->signature?></li>
                                <li>真实姓名：<?=$model->realname?></li>
                                <li>手机：<?=$model->mobile?> <?=$model->is_mobile_verify?'(已验证)':''?></li>
                                <li>邮箱：<?=$model->email?> <?=$model->is_email_verify?'(已验证)':''?></li>
                                <li>所在城市：<?=$tmp_name?></li>
                                <li>详细地址：<?=$model->address?></li>
                                <li>关注行业：<?=$model->follow_industry?></li>
                            </ul>
                        </div>
                    </div>
                </div>
                <div class="right_item" id="edit_user" style="display: none;">
                    <div class="row">
                        <div class="col-md-2">
                            <span class="right_icon"><img src="<?=$model->avatar?>" onerror="this.src='/static/frontend/image/noavatar.png';"/></span>
                        </div>
                        <div class="col-md-10">
                            <?php
                            $form = ActiveForm::begin([
                                'id' => 'basic-form'
                            ])
                            ?>
                            <ul class="right_item_list">
                                <li class="flex"><label>昵称：</label><input type="text" name="nickname" id="nickname" value="<?=$model->nickname?>" /><i class="t_red">*</i></li>
                                <li class="flex"><label>个性签名：</label><input type="text" name="signature" value="<?=$model->signature?>" /></li>
                                <li class="flex"><label>真实姓名：</label><input type="text" name="realname" id="realname" value="<?=$model->realname?>" /><i class="t_red">*</i></li>
                                <li class="flex">
                                    <label>手机：</label>
                                    <input type="text" name="mobile" id="mobile" value="<?=$model->mobile?>" />
<!--                                    <span class="t_bule">修改</span>-->
                                </li>
                                <li class="flex" style="display: none;">
                                    <label>手机验证码：</label>
                                    <input type="text" class="sm" name="mobile_verify" value="" />
                                </li>
                                <li class="flex">
                                    <label>邮箱：</label>
                                    <input type="text" name="email" id="email" value="<?=$model->email?>" />
<!--                                    <span class="t_bule">修改</span>-->
                                </li>
                                <li class="flex" style="display: none;">
                                    <label>邮箱验证码：</label>
                                    <input type="text" name="email_verify" value=""  />
                                </li>
                                <li class="flex">
                                    <label>所在城市：</label>
                                    <select name="province" class="province" id="province" onchange="getRegion(this, 'city')">
                                        <option value=""><?=Yii::t('common', 'select...')?></option>
                                    </select>
                                    <select name="city" class="city" id="city">
                                        <option value=""><?=Yii::t('common', 'select...')?></option>
                                    </select>
                                </li>
                                <li class="flex"><label>详细地址：</label><input type="text" name="address" value="<?=$model->address?>" /><i class="t_red">*</i></li>
                                <li class="flex">
                                    <label>关注行业：</label>
                                    <select class="big" name="follow_industry" id="follow_industry">
                                        <option value=""><?=Yii::t('common', 'select...')?></option>
                                        <?php
                                        if (!empty($industry)) {
                                            foreach ($industry as $val) {
                                        ?>
                                        <option value="<?=$val['id']?>" <?=$val['name']==$model->follow_industry?'selected="selected"':''?>><?=$val['name']?></option>
                                        <?php
                                            }
                                        }
                                        ?>
                                    </select>
                                    <i class="t_red">*</i>
                                </li>
                                <li class="flex">
                                    <label></label>
                                    <div>
                                        <button type="submit" id="save_basic" class="btn_red pointer_btn">保存</button>
                                        <button type="reset" class="btn_black pointer_btn cancel_edit_user">取消</button>
                                    </div>
                                </li>
                            </ul>
                            <?php
                            ActiveForm::end();
                            ?>
                        </div>
                    </div>
                </div>
                <?php
                if (!empty($userCompany)) {
                ?>
                <div class="right_title flex f_between">
                    <span class="right_t_label t_red">公司资料</span>
                    <span class="right_btn" id="edit_company_btn"><i class="glyphicon glyphicon-pencil"></i><em>修改公司资料</em></span>
                </div>
                <div class="right_item" id="original_company">
                    <div class="row">
                        <div class="col-md-2"></div>
                        <div class="col-md-10">
                            <ul class="right_item_list">
                                <li>公司名称：<?=$userCompany->name?></li>
                                <li>公司网址：<?=$userCompany->url?></li>
                                <li>公司电话：<?=$userCompany->telphone?></li>
                                <li>公司传真：<?=$userCompany->fax?></li>
                                <li>主营产品：<?=$userCompany->product?></li>
                                <li>展会分类：<?=$userCompany->category?></li>
                            </ul>
                        </div>
                    </div>
                </div>
                <div class="right_item" id="edit_company" style="display: none;">
                    <div class="row">
                        <div class="col-md-2"></div>
                        <div class="col-md-10">
                            <?php
                            $form1 = ActiveForm::begin([
                                'id' => 'company-form',
                                'action' => ['/member/save-company'],
                            ])
                            ?>
                            <ul class="right_item_list">
                                <li class="flex">
                                    <label>公司名称：</label>
                                    <?=$form->field($userCompany, 'name')->textInput(['class' => 'form_edit'])->label(false)?>
                                </li>
                                <li class="flex">
                                    <label>公司网址：</label>
                                    <?=$form->field($userCompany, 'url')->textInput(['id' => 'url', 'class' => 'form_edit'])->label(false)?>
                                </li>
                                <li class="flex">
                                    <label>公司电话：</label>
                                    <?=$form->field($userCompany, 'telphone')->textInput(['class' => 'form_edit'])->label(false)?>
                                </li>
                                <li class="flex">
                                    <label>公司传真：</label>
                                    <?=$form->field($userCompany, 'fax')->textInput(['class' => 'form_edit'])->label(false)?>
                                </li>
                                <li class="flex">
                                    <label>主营产品：</label>
                                    <?=$form->field($userCompany, 'product')->textInput(['class' => 'form_edit'])->label(false)?><em>多个请用","隔开</em>
                                </li>
                                <li class="flex">
                                    <label>展会分类：</label>
                                    <?=$form->field($userCompany, 'category_id')->dropDownList($exhibitionCategory, ['class' => 'form_edit'])->label(false)?>
                                </li>
                                <li class="flex">
                                    <label></label>
                                    <div>
                                        <?=Html::submitButton("保存", ['class' => 'btn_red pointer_btn', 'id' => 'submit-company'])?>
                                        <?=Html::resetButton("取消", ['class' => 'btn_black pointer_btn cancel_edit_company'])?>
                                    </div>
                                </li>
                            </ul>
                            <?php
                            ActiveForm::end();
                            ?>
                        </div>
                    </div>
                </div>
                <?php
                }
                ?>
            </div>
        </div>
    </div>
</div>
<script>
$(function () {
    $("#edit_user_btn").on('click', function(e){
        if ($(this).hasClass('active')) {
            $(this).find('em').text('修改个人资料');
            $("#original_user").show();
            $("#edit_user").hide();
            $(this).removeClass('active')
        }else{
            $(this).find('em').text('取消修改');
            $(this).addClass('active');
            $("#edit_user").show();
            $("#original_user").hide();
        }
    })
    $("#edit_company_btn").on('click', function(e){
        if ($(this).hasClass('active')) {
            $(this).find('em').text('修改公司资料');
            $("#original_company").show();
            $("#edit_company").hide();
            $(this).removeClass('active')
        }else{
            $(this).find('em').text('取消修改');
            $(this).addClass('active');
            $("#edit_company").show();
            $("#original_company").hide();
        }
    })
    $(".cancel_edit_user").on('click', function(e){
        $("#edit_user_btn").find('em').text('修改个人资料').removeClass('active');
        $("#original_user").show();
        $("#edit_user").hide();
    })
    $(".cancel_edit_company").on('click', function(e){
        $("#edit_company_btn").find('em').text('修改公司资料').removeClass('active');
        $("#original_company").show();
        $("#edit_company").hide();
    })

    $("#save_basic").on('click', function (e) {
        var nickname = $("#nickname").val();
        if (nickname == '') {
            alert('请输入昵称');
            return false;
        }
        var xss = /[\':;*?~`!@#$%^&+={}\[\]\<\>\(\),\.]/;
        if(xss.test(nickname)){
            alert('昵称包含特殊字符');
            return false;
        }

        var realname = $("#realname").val();
        if (realname == '') {
            alert('请输入真实姓名');
            return false;
        }
        if(xss.test(realname)){
            alert('真实姓名包含特殊字符');
            return false;
        }
        var mobile = $("#mobile").val();
        if (!isMobile(mobile)) {
            alert('手机号码格式错误');
            return false;
        }
        var email = $("#email").val();
        if (!isEmail(email)) {
            alert('邮箱格式错误');
            return false;
        }
        var city = $("#city").val();
        if (city == '') {
            alert('请选择城市');
            return false;
        }
        var address = $("#address").val();
        if (address == '') {
            alert('请输入详细地址');
            return false;
        }
        if(xss.test(address)){
            alert('详细地址包含特殊字符');
            return false;
        }
        var industry = $("#follow_industry").val();
        if (industry == '') {
            alert('请选择关注行业');
            return false;
        }
        $.ajax({
            url: '<?=Yii::$app->urlManager->createUrl(['member/info'])?>',
            data: $("#basic-form").serialize(),
            dataType: 'JSON',
            type: 'POST',
            async: true,
            success: function (response) {
                if (response.code == 0) {
                    alert('保存成功');
                    location.reload();
                }else{
                   alert(response.msg);
                }
            },
            error: function (error) {
                alert(JSON.stringify(error));
                return false;
            }
        })

        return false;
    })
    $("#submit-company").on('click', function (e) {
        e.preventDefault();
        $.ajax({
            url: $("#company-form").attr('action'),
            data: $("#company-form").serialize(),
            dataType: 'JSON',
            type: 'POST',
            async: true,
            success: function (response) {
                if (response.code == 0) {
                    alert('保存成功');
                    location.reload();
                }else{
                   alert(response.msg);
                }
            },
            error: function (error) {
                alert(JSON.stringify(error));
                return false;
            }
        })

        return false;
    })
})

getRegion(0, 'province');
function getRegion(obj, id){
    var pid = typeof obj == 'object' ? obj.value : obj;
    pid = typeof pid == 'undefined' || pid == '' ? 0 : pid;
    $.get("<?=Yii::$app->urlManager->createUrl(['/common/get-region'])?>", {pid: pid}, function (response) {
        if (response.code == 0) {
            var data = response.data;
            var html = '';
            if (data !='' && typeof data == 'object') {
                html += '<option value=""><?=Yii::t('common', 'select...')?></option>';
                for (var i in data) {
                    var selectd = false;
                    <?php
                    if (!empty($province_id)){
                    ?>
                    if (id == 'province' && i == parseInt('<?=$province_id?>')){
                        selectd = true;
                    }
                    <?php
                    }
                    ?>
                    <?php
                    if (!empty($city_id)){
                    ?>
                    if (id == 'city' && i == parseInt('<?=$city_id?>')){
                        selectd = true;
                    }
                    <?php
                    }
                    ?>
                    html += '<option value="'+i+'" '+(selectd?'selected="selected"':'')+'>'+data[i]+'</option>';
                }
                $("#"+id).html(html).parent().show();
            }else {
                $("#" + id).html(html).parent().hide();
            }
        }
    }, 'json')
}
<?php
if (!empty($city_id)){
?>
getRegion(<?=$province_id?>, 'city');
<?php
}
?>
</script>